package com.naiking.core
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.system.System;
	
	/**
	 * @author Nai.King
	 * 创建时间：2013-10-21 上午10:04:23
	 * 位图序列的MC
	 */
	public class NMovieClip extends Sprite
	{
		private var _frmVec:Vector.<BitmapData>;
		private var _bmpdt:BitmapData;
		private var _bmp:Bitmap;
		private var _repeat:int;
		private var _cntTimes:int;
		private var _cntFrm:int;
		private var _totalFrm:int;
		private var _gc:Boolean;
		public static const EVT_PLAYCOMPLETE:String="EVT_PLAYCOMPLETE";
		public function NMovieClip(gc:Boolean=false)
		{
			super();
			_gc=gc;
			_bmp=new Bitmap();
			addChild(_bmp);
			_cntFrm=0;
			_cntTimes=0;
			this.mouseChildren=false;
		}
		public function play(repeat:int):void
		{
			_repeat=repeat;
			this.removeEventListener(Event.ENTER_FRAME,onLoop);
			if(_totalFrm>1)
			{
				this.addEventListener(Event.ENTER_FRAME,onLoop);
			}else
			{
				_bmp.bitmapData=_frmVec[0];
			}
		}
		public function dispose():void
		{
			this.removeEventListener(Event.ENTER_FRAME,onLoop);
			if(_gc&&_frmVec)
			{
				for each(var bmpdt:BitmapData in _frmVec)
				{
					bmpdt.dispose();
					bmpdt=null;
				}
			}
			_frmVec=null;
			_cntFrm=0;
			_cntTimes=0;
		}
		private function onLoop(e:Event):void
		{
			_cntFrm++;
			_bmp.bitmapData=_frmVec[_cntFrm-1];
			if(_cntFrm==_totalFrm)
			{
				_cntFrm=0;
				_cntTimes++;
				if(_cntTimes==_repeat)
				{
					//播放结束了 停止enterfame
					this.removeEventListener(Event.ENTER_FRAME,onLoop);
					dispatchEvent(new Event(EVT_PLAYCOMPLETE));
				}
			}
		}
		/**
		 *设置MC的序列帧的位图数据 
		 * @param url 键（url）
		 * @param bmpdts
		 * 
		 */		
		public function setFramedBmpdatas(bmpdts:Vector.<BitmapData>):void
		{
			_cntFrm=0;
			_cntTimes=0;
			_frmVec=bmpdts;
			_totalFrm=_frmVec.length;
		}
	}
}